package com.ruoyi.system.controller.person;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.Econtract;
import com.ruoyi.system.domain.Emplyee;
import com.ruoyi.system.domain.Evection;
import com.ruoyi.system.dto.ChangeStatusDto;
import com.ruoyi.system.dto.EvectionDto;
import com.ruoyi.system.service.IEcontractService;
import com.ruoyi.system.service.IEmplyeeService;
import com.ruoyi.system.service.IEvectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.mappers.SecurityMapperImpl;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 出警
 *
 * @author ruoyi
 * @date 2023-10-09
 */
@RestController
@RequestMapping("/evection")
public class EvectionController extends BaseController
{
    @Autowired
    private IEvectionService evectionService;
    @Autowired
    private IEmplyeeService emplyeeService;

    /**
     * 查询出差申请列表
     */
    @RequiresPermissions("person:evection:list")
    @GetMapping("/list")
    public TableDataInfo list(EvectionDto dto)
    {
        System.out.println("出差申请："+dto);
        startPage();
        Evection evection = new Evection();
        evection.setName(dto.getName());
        List<Evection> list = evectionService.selectEvectionList(evection);
        return getDataTable(list);
    }

    /**
     * 导出出差申请列表
     */
    @RequiresPermissions("person:evection:export")
    @Log(title = "出差申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Evection evection)
    {
        List<Evection> list = evectionService.selectEvectionList(evection);
        ExcelUtil<Evection> util = new ExcelUtil<Evection>(Evection.class);
        util.exportExcel(response, list, "出差申请数据");
    }

    /**
     * 获取出差申请详细信息
     */
    @RequiresPermissions("person:evection:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(evectionService.selectEvectionById(id));
    }

    /**
     * 新增出差申请
     */
//    @RequiresPermissions("person:evection:add")
    @Log(title = "出差申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Evection evection)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        System.out.println("sysUser:"+sysUser);
        Emplyee emplyee = emplyeeService.selectEmplyeeByEno(sysUser.getEno());
        System.out.println("当前登录员工："+emplyee.getName());
        evection.setName(emplyee.getName());
        evection.setEno(sysUser.getEno());
        evection.setStatus(1L);
        return toAjax(evectionService.insertEvection(evection));
    }

    /**
     * 修改出差申请
     */
    @RequiresPermissions("person:evection:edit")
    @Log(title = "出差申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Evection evection)
    {
        return toAjax(evectionService.updateEvection(evection));
    }

    /**
     * 修改审批状态
     */
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ChangeStatusDto dto)
    {
        Evection evection = new Evection();
        evection.setId(dto.getId());
        evection.setStatus(dto.getStatus());
        return toAjax(evectionService.updateEvection(evection));
    }

    /**
     * 删除出差申请
     */
    @RequiresPermissions("person:evection:remove")
    @Log(title = "出差申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(evectionService.deleteEvectionByIds(ids));
    }
}
